<?php
/**
 * Created by PhpStorm.
 * User: jiangjunxian
 * Date: 28/11/2016
 * Time: 13:04
 */

namespace app\modules\admin\components;


use Yii;
use yii\base\Object;
use yii\caching\Cache;
use yii\db\Connection;
use yii\di\Instance;
use yii\helpers\ArrayHelper;

class Configs extends Object
{
    const CACHE_TAG = 'jjx.admin.cache';

    /**
     * @var Connection 数据库连接组件;
     */
    public $db = 'db';
    /**
     * @var Cache 缓存组件;
     */
    public $cache = 'cache';
    /**
     * @var int 缓存持续时间，默认1小时
     */
    public $cacheDuration = 3600;
    /**
     * @var string 菜单表表名
     */
    public $menuTable = '{{%menu}}';
    /**
     * @var string 用户表表名
     */
    public $userTable = '{{%user}}';
    /**
     * @var string IdentityClass
     */
    public $identityClass = null;
    /**
     * @var int 用户注册以后的默认状态，10表示是激活的用户
     */
    public $defaultUserStatus = 10;
    /**
     * @var bool 如果值为true,只要注册过的路由,都能通过验证
     */
    public $onlyRegisteredRoute = false;
    /**
     * @var bool 严格权限检查,如果为false,AccessControl将不检查Rule.
     */
    public $strict = true;
    /**
     * @var array
     */
    public $options;
    /**
     * @var self Instance of self;
     */
    private static $_instance;
    private static $_classes = [
        'db' => 'yii\db\Connection',
        'cache' => 'yii\caching\Cache',
    ];

    /**
     * 初始化配置类
     */
    public function init()
    {
        foreach (self::$_classes as $key => $class) {
            try {
                $this->{$key} = empty($this->{$key}) ? null : Instance::ensure($this->{$key}, $class);
            } catch (\Exception $exception) {
                $this->{$key} = null;
                Yii::error($exception->getMessage());
            }
        }
        $this->identityClass = $this->identityClass ?: Yii::$app->user->identityClass;
    }

    /**
     * 单例方式获取实例
     * @return Configs|object
     */
    public static function instance() {
        if(self::$_instance === null) {
            $type = ArrayHelper::getValue(Yii::$app->params, 'jjx.admin.configs', []);
            if(is_array($type) && !isset($type['class'])) {
                $type['class'] = static::className();
            }
            return self::$_instance = Yii::createObject($type);
        }
        return self::$_instance;
    }

    /**
     *
     * @param $name
     * @param $arguments
     * @return mixed|null
     */
    public static function __callStatic($name, $arguments)
    {
        $instance = static::instance();
        if($instance->hasProperty($name)) {
            return $instance->$name;
        } else {
            if(count($arguments)) {
                $instance->options[$name] = reset($arguments);
            } else {
                return array_key_exists($name, $instance->options) ? $instance->options[$name] : null;
            }
        }
    }

    /**
     * 数据库连接组件
     * @return Connection
     */
    public static function db() {
        return static::instance()->db;
    }

    /**
     * 缓存组件
     * @return Cache
     */
    public static function cache() {
        return static::instance()->cache;
    }

    /**
     * 菜单表表名
     * @return string
     */
    public static function menuTable() {
        return static::instance()->menuTable;
    }

    /**
     * @return string
     */
    public static function userTable() {
        return static::instance()->userTable;
    }

    /**
     * @return string
     */
    public static function identityClass() {
        return static::instance()->identityClass;
    }

    /**
     * @return int
     */
    public static function defaultUserStatus() {
        return static::instance()->defaultUserStatus;
    }

    /**
     * @return bool
     */
    public static function onlyRegisteredRoute() {
        return static::instance()->onlyRegisteredRoute;
    }

    /**
     * @return bool
     */
    public static function strict() {
        return static::instance()->strict;
    }

	/**
	 * 说明:
	 * @return int
	 */
	public static function cacheDuration() {
    	return static::instance()->cacheDuration;
	}
}